LICENSE = "Apache-2.0"
LIC_FILES_CHKSUM = "file://LICENSE;md5=86d3f3a95c324c9479bd8986968f4327"

SRC_URI = "git://gitee.com/openbmc_source/gpio-manager.git;protocol=https;branch=master"
SRC_URI:append = " file://gpio_defs.json"

SRCREV = "a3ab06498cfce5eaefdad98865d346e12f903cf4"

PV = "1.0+git${SRCPV}"
S = "${WORKDIR}/git"

inherit meson systemd
inherit obmc-phosphor-dbus-service
inherit obmc-phosphor-systemd

DEPENDS += "\
            sdbusplus \
            libgpiod \
            nlohmann-json \
            "
RDEPENDS:${PN} = "bash phosphor-state-manager"

GPIO_MANAGER_FMT = "gpio-manager@{0}.service"
POWER_ON_FMT = "power-on@{0}.service"
POWER_OFF_FMT = "power-off@{0}.service"
OP_POWERED_OFF_FMT = "op-powered-off@{0}.service"
OP_RESET_CHASSIS_ON = "op-reset-chassis-on@{0}.service"
OP_RESET_CHASSIS_RUNNING = "op-reset-chassis-running@{0}.service"

SYSTEMD_SERVICE:${PN} = " \
                          ${@compose_list(d, 'GPIO_MANAGER_FMT', 'OBMC_POWER_INSTANCES')} \
                          ${@compose_list(d, 'POWER_ON_FMT', 'OBMC_POWER_INSTANCES')} \
                          ${@compose_list(d, 'POWER_OFF_FMT', 'OBMC_POWER_INSTANCES')} \
                          ${@compose_list(d, 'OP_POWERED_OFF_FMT', 'OBMC_CHASSIS_INSTANCES')} \
                          ${@compose_list(d, 'OP_RESET_CHASSIS_ON', 'OBMC_CHASSIS_INSTANCES')} \
                          ${@compose_list(d, 'OP_RESET_CHASSIS_RUNNING', 'OBMC_CHASSIS_INSTANCES')} \
                          "

FILES:${PN} = " \
                ${systemd_system_unitdir} \
                ${bindir}/gpiomanagement \
                ${bindir}/gpioutil \
                /etc/default/obmc/gpio \
                "

CHASSIS_ON_TMPL = "obmc-chassis-poweron@.target"
CHASSIS_ON_TGTFMT = "obmc-chassis-poweron@{1}.target"

CHASSIS_OFF_TMPL = "obmc-chassis-poweroff@.target"
CHASSIS_OFF_TGTFMT = "obmc-chassis-poweroff@{1}.target"

# Now show that the main control target requires these power targets
HOST_STARTMIN_TMPL = "obmc-host-startmin@.target"
HOST_STARTMIN_TGTFMT = "obmc-host-startmin@{0}.target"
HOST_STARTMIN_FMT = "../${CHASSIS_ON_TMPL}:${HOST_STARTMIN_TGTFMT}.requires/${CHASSIS_ON_TGTFMT}"
SYSTEMD_LINK:${PN} += "${@compose_list_zip(d, 'HOST_STARTMIN_FMT', 'OBMC_POWER_INSTANCES', 'OBMC_CHASSIS_INSTANCES')}"

# Chassis off requires host off
HOST_STOP_TMPL = "obmc-host-stop@.target"
HOST_STOP_INSTFMT = "obmc-host-stop@{0}.target"
HOST_STOP_FMT = "../${HOST_STOP_TMPL}:${CHASSIS_OFF_TGTFMT}.requires/${HOST_STOP_INSTFMT}"
SYSTEMD_LINK:${PN} += "${@compose_list_zip(d, 'HOST_STOP_FMT', 'OBMC_HOST_INSTANCES', 'OBMC_CHASSIS_INSTANCES')}"

# Hard power off requires chassis off
HARD_OFF_TGTFMT = "obmc-chassis-hard-poweroff@{0}.target"
HARD_OFF_FMT = "../${CHASSIS_OFF_TMPL}:${HARD_OFF_TGTFMT}.requires/${CHASSIS_OFF_TGTFMT}"
SYSTEMD_LINK:${PN} += "${@compose_list_zip(d, 'HARD_OFF_FMT', 'OBMC_CHASSIS_INSTANCES', 'OBMC_CHASSIS_INSTANCES')}"

# Force the standby target to run the chassis reset check target
RESET_TMPL_CTRL = "obmc-chassis-powerreset@.target"
SYSD_TGT = "multi-user.target"
RESET_INSTFMT_CTRL = "obmc-chassis-powerreset@{0}.target"
RESET_FMT_CTRL = "../${RESET_TMPL_CTRL}:${SYSD_TGT}.wants/${RESET_INSTFMT_CTRL}"
SYSTEMD_LINK:${PN} += "${@compose_list_zip(d, 'RESET_FMT_CTRL', 'OBMC_CHASSIS_INSTANCES')}"

do_install:append() {
    install -d ${D}/etc/default/obmc/gpio
    install -m 0644 ${WORKDIR}/gpio_defs.json ${D}/etc/default/obmc/gpio
}
